﻿using RabbitMQ.Client;
using System;
using System.Text;

namespace _008_RabbitMqSend
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("欢迎进入消息输入栏!");

            ConnectionFactory factory = new ConnectionFactory();
            factory.HostName = "192.168.16.102";
            factory.UserName = "admin";
            factory.Password = "admin";

            using (var con = factory.CreateConnection())
            {
                using (var route1 = con.CreateModel())
                {
                    //route1.ExchangeDelete("myExchange");
                    ////生产者-消费者模式
                    //Product_Consume_Send(route1);

                    //广播模式
                    Fanout_Send(route1);
                }
            }
        }

        private static void Fanout_Send(IModel route1)
        {
            string exchangeName = "myExchange";
            route1.ExchangeDeclare(exchangeName, "fanout", false, true);

            string str = Console.ReadLine();
            while (str != "q")
            {
                string msg = str;
                route1.BasicPublish(exchangeName, "", null, Encoding.UTF8.GetBytes(msg));
                Console.WriteLine($"Send Fanout Message:{msg}");
                str = Console.ReadLine();
            }
        }

        private static void Product_Consume_Send(IModel route1)
        {
            string routeName = "myRoute1";
            route1.QueueDeclare(routeName, false, false, true, null);
            var property = route1.CreateBasicProperties();
            property.DeliveryMode = 1;

            string str = Console.ReadLine();
            while (str != "q")
            {
                string msg = str;
                route1.BasicPublish("", routeName, property, Encoding.UTF8.GetBytes(msg));
                Console.WriteLine($"Send Message:{msg}");
                str = Console.ReadLine();
            }
        }
    }
}
